package com.kenyi.co.ui.me.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnNetWorkErrorListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.google.gson.Gson;
import com.kenyi.co.R;
import com.kenyi.co.common.ui.BaseActivity;
import com.kenyi.co.okhttp.OkHttpUtils;
import com.kenyi.co.okhttp.callback.StringCallback;
import com.kenyi.co.ui.me.adapter.MainAvatarAdapter;
import com.kenyi.co.ui.me.bean.AvatarBean;
import com.kenyi.co.ui.me.bean.AvatarInfoBean;
import com.kenyi.co.ui.me.bean.AvatarListBean;
import com.kenyi.co.utils.LogU;
import com.kenyi.co.utils.NetConfig;
import com.kenyi.co.utils.SharepUtils;
import com.kenyi.co.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;


/**
 * 设置头像
 */
public class MeAvatarActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "MeAvatarActivity";
    private TextView tv_home_title;
    private Button li_footer;
    private Toolbar toolbar;
    private List<String> itemList;
    private String savatar;
    private List<AvatarListBean> itemListimg;
    private int pos = 0;
    private LRecyclerView mRecyclerView = null;
    private LRecyclerViewAdapter mLRecyclerViewAdapter = null;
    private MainAvatarAdapter mDataAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_avatar);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        savatar = SharepUtils.getString_info(this,SharepUtils.USER_LOGIN_HEADADDRESS);

        initView();
        mRecyclerView = (LRecyclerView) findViewById(R.id.list);
        mDataAdapter = new MainAvatarAdapter(MeAvatarActivity.this);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter);
        mRecyclerView.setAdapter(mLRecyclerViewAdapter);
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.default_divider_height_10)
                .setPadding(R.dimen.default_divider_padding_10)
                .setColorResource(R.color.white)
                .build();
        mRecyclerView.addItemDecoration(divider);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
        mRecyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
        //是否禁用自动加载更多功能,false为禁用, 默认开启自动加载更多功能
        mRecyclerView.setLoadMoreEnabled(false);
        mRecyclerView.setPullRefreshEnabled(false);

        mRecyclerView.setOnNetWorkErrorListener(new OnNetWorkErrorListener() {
            @Override
            public void reload() {

            }
        });
        mRecyclerView.refresh();

        LinearLayout adView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.activity_me_footer, null);
        li_footer = adView.findViewById(R.id.bt_summit);
        li_footer.setOnClickListener(this);
        mLRecyclerViewAdapter.addFooterView(adView);
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                AvatarListBean item = mDataAdapter.getDataList().get(position);

                for (int i = 0; i < itemListimg.size(); i++) {
                    itemListimg.get(i).setIsselect(false);
                }
                itemListimg.get(position).setIsselect(true);
                mLRecyclerViewAdapter.notifyDataSetChanged();
                pos = position;
//                view.findViewById(R.id.iv_me_main_avatar_select).setVisibility(View.VISIBLE);
            }
        });
        getHeadPortrait();
    }

    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.white));//设置状态栏颜色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
        }
    }
    private void initView() {
        setStatusBar();
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_home_title = (TextView) findViewById(R.id.tv_home_title);
        tv_home_title.setText("个人头像");


    }

    @Override
    public void onResume() {
        super.onResume();

        MobclickAgent.onPageStart("个人头像");
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();

        MobclickAgent.onPageEnd("个人头像");
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected void onStart() {
        super.onStart();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.bt_summit:
                setHeadPortrait(itemList.get(pos));

                break;
        }
    }

    /**
     * 获取系统头像库
     */
    public void getHeadPortrait() {

        Map<String, String> params = new HashMap<>();

        params.put("page", "");

        Map<String, String> headers = new HashMap<>();
        headers.put("APP-Key", "APP-Secret");
        headers.put("Authorization", SharepUtils.getString_info(MeAvatarActivity.this, SharepUtils.USER_LOGIN_TOKEN));


        String url = NetConfig.APPUSER_HEADPORTRAIT;

        OkHttpUtils.get()
                .url(url)
                .params(params)
                .headers(headers)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                        mRecyclerView.refreshComplete(10);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();

                        int num = 0;
                        AvatarBean packlist = gson.fromJson(response, AvatarBean.class);
                        int code = packlist.getCode();
                        if (code == 200) {
                            itemList = new ArrayList<String>();
                            itemList = packlist.getDatas().getImgList();

                            itemListimg = new ArrayList<AvatarListBean>();
                            for (int i = 0; i < itemList.size(); i++) {
                                AvatarListBean avatar = new AvatarListBean();
                                avatar.setAvatarimg(itemList.get(i));
                                if (itemList.get(i).equals(savatar)) {
                                    avatar.setIsselect(true);
                                    pos = i;
                                }
                                itemListimg.add(avatar);
                            }
                            num = itemList.size();
                            mDataAdapter.addAll(itemListimg);
                            mRecyclerView.setVisibility(View.VISIBLE);
                            mRecyclerView.refreshComplete(num);
                        }


                    }
                });
    }

    /**
     * 修改头像
     */
    public void setHeadPortrait(String data) {

        Map<String, String> params = new HashMap<>();

        params.put("userId", SharepUtils.getString_info(MeAvatarActivity.this, SharepUtils.USER_LOGIN_USERID));
        params.put("headPortraitUrl", data);

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Authorization", SharepUtils.getString_info(MeAvatarActivity.this, SharepUtils.USER_LOGIN_TOKEN));


        String url = NetConfig.APPUSER_MODIFYHEADPORTRAIT;

        OkHttpUtils.post()
                .url(url)
                .params(params)
                .headers(headers)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                        mRecyclerView.refreshComplete(10);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();


                        AvatarInfoBean packlist = gson.fromJson(response, AvatarInfoBean.class);
                        int code = packlist.getCode();
                        if (code == 200) {
                            SharepUtils.setString_info(MeAvatarActivity.this, packlist.getDatas().getNewHeadPortrait(), SharepUtils.USER_LOGIN_HEADADDRESS);
                            finish();
                        }


                    }
                });
    }

}
